﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AugustSql
{
    public class AugustOrmFactory
    {

        public static T Register<T>(string database = "database", string primaryKey="Index", string ipAddress = "127.0.0.1", string userName = "root", string passWord = "123456", int port = 3306) where T : ISqlCollectionBase
        {
            ISqlCollectionBase repo = Activator.CreateInstance<T>();
            if (!MySqlHelper.ExsitDatabase(database, ipAddress, userName, passWord, port))
                MySqlHelper.CreatDataBase(database, ipAddress, userName, passWord, port);
            var atr = repo.ResgistType.GetCustomAttributes(false);
            bool safe = true;
            foreach (var item in atr)
            {
                if (item is UnsafeTableAttribute tn)
                {
                    safe = false;
                    break;
                }
            }
            foreach (var item in atr)
            {
                if (item is TableAttribute tn)
                {
                    SqlInstance sqlInstance = SqlService.Connect(database, ipAddress, primaryKey, userName, passWord, port);
                    return (T)sqlInstance.Register(repo, primaryKey,safe);
                }
            }
            throw new Exception($"Register方法参数中不包含表名，且{typeof(T).Name}未标记 Table 特性");
        }


    }
}
